package DOM4J;

import java.io.File;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class DOM4JXpath {
	public static void main(String[] args) {
		try {
			readList2();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void readSingleNode() throws DocumentException,
			XPathExpressionException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		Element root = (Element) document.selectSingleNode("/书架/书[1]");
		String name = root.attributeValue("name");
		System.out.println(name);
	}

	public static void readList() throws DocumentException,
			XPathExpressionException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		List root = document.selectNodes("//售价/parent::*");
		for (int i = 0; i < root.size(); i++) {
			Element bookname = (Element) root.get(i);
			System.out.println(bookname.getText()+"    bookname.getName()= "+ bookname.getName());
		}
	}
	public static void readList2() throws DocumentException,
	XPathExpressionException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("C:\\a.plmxml"));
		List root = document.selectNodes("/PLMXML/ProductDef");//PLMXML/ProductDef/InstanceGraph/ProductRevisionView
		for (int i = 0; i < root.size(); i++) {
			Element t = (Element) root.get(i);
			System.out.println( t.attributeValue("id")+" ");
		}
	}
}
